package com.wx.chapter10.single_;

/**
 * ClassName:SingleTon02
 * Package :com.wx.chapter10.single_
 * Description :
 *
 * @Author : coder_mu
 * @Create : 2023/6/4 - 20:39
 */
public class SingleTon02 {
    public static void main(String[] args) {
        Cat cat1 = Cat.getInstance();
        Cat cat2 = Cat.getInstance();
        System.out.println(Cat.getInstance());
        System.out.println(cat1 == cat2);
    }
}

class Cat {
    private String name;
    private static Cat cat;//不直接创建对象

    private Cat(String name) {
        this.name = name;
    }

    //共有方法进行判断，然后创建对象
    public static Cat getInstance() {
        if (cat == null) {
            cat = new Cat("小可爱");
        }
        return cat;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                '}';
    }
}